set -x
ENGINE=${1:-vllm}
# export VLLM_ATTENTION_BACKEND=XFORMERS
export HF_ENDPOINT=https://hf-mirror.com
export HYDRA_FULL_ERROR=1
export RAY_DEBUG=0
export ASCEND_LAUNCH_BLOCKING=0
export USE_VLLM_V1=1
export GLOO_SOCKET_IFNAME=bond0

train_data_size=2
val_data_size=2
# val_data_size=16
group_size=2

DATE=$(date +%Y%m%d_%H%M%S)
project_name="toolbench"
experiment_name="grpo_qwen2.5_32b_$DATE"


export TENSORBOARD_DIR="log/$project_name/$experiment_name/"


# We only use data preparation to indicate the modality and the data size.
python3 -m examples.data_preprocess.prepare \
    --mode 'text' \
    --train_data_size $train_data_size \
    --val_data_size $val_data_size

python3 -m verl.trainer.main_ppo \
    --config-name="ppo_megatron_trainer.yaml" \
    algorithm.adv_estimator=grpo \
    data.train_files=$HOME/data/verl-agent/text/train.parquet \
    data.val_files=$HOME/data/verl-agent/text/test.parquet \
    data.train_batch_size=$train_data_size \
    data.val_batch_size=$val_data_size \
    data.max_prompt_length=6000 \
    data.max_response_length=2048 \
    data.filter_overlong_prompts=True \
    data.truncation='middle' \
    data.return_raw_chat=True \
    actor_rollout_ref.model.path=/mnt/nas/huggingface_hub/models--Qwen--Qwen2.5-0.5B-Instruct \
    actor_rollout_ref.model.enable_gradient_checkpointing=True \
    actor_rollout_ref.actor.optim.lr=1e-6 \
    actor_rollout_ref.actor.use_torch_compile=False \
    actor_rollout_ref.actor.ppo_micro_batch_size_per_gpu=2 \
    actor_rollout_ref.actor.use_kl_loss=True \
    actor_rollout_ref.actor.kl_loss_coef=0.01 \
    actor_rollout_ref.actor.kl_loss_type=low_var_kl \
    actor_rollout_ref.actor.megatron.tensor_model_parallel_size=2 \
    actor_rollout_ref.actor.ppo_mini_batch_size=2 \
    actor_rollout_ref.rollout.log_prob_micro_batch_size_per_gpu=2 \
    actor_rollout_ref.rollout.tensor_model_parallel_size=4 \
    actor_rollout_ref.rollout.name=$ENGINE \
    actor_rollout_ref.rollout.max_model_len=12000 \
    actor_rollout_ref.rollout.gpu_memory_utilization=0.6 \
    actor_rollout_ref.rollout.enable_chunked_prefill=False \
    actor_rollout_ref.rollout.enforce_eager=False \
    actor_rollout_ref.rollout.free_cache_engine=False \
    actor_rollout_ref.rollout.val_kwargs.temperature=0.4 \
    actor_rollout_ref.rollout.val_kwargs.do_sample=True \
    actor_rollout_ref.ref.log_prob_micro_batch_size_per_gpu=1 \
    algorithm.use_kl_in_reward=False \
    env.env_name=toolbench \
    env.seed=0 \
    env.max_steps=2 \
    env.rollout.n=$group_size \
    trainer.critic_warmup=0 \
    trainer.device=npu \
    trainer.logger=['console',"tensorboard"] \
    trainer.project_name="$project_name" \
    trainer.experiment_name="$experiment_name" \
    trainer.default_local_dir="checkpoints/$project_name/$experiment_name" \
    trainer.rollout_data_dir="log/$project_name/$experiment_name/rollout" \
    trainer.validation_data_dir="log/$project_name/$experiment_name/val" \
    trainer.tensorboard_dir="log/$project_name/$experiment_name/" \
    trainer.n_gpus_per_node=2 \
    trainer.nnodes=1 \
    trainer.save_freq=5 \
    trainer.test_freq=5 \
    trainer.total_epochs=150 \
    trainer.log_val_generations=4 \
    trainer.val_before_train=True 2>&1 | tee debug_mega.log
